<template>
  <div>
    <el-main>
      <pre>
          
    根据属性分组

    const arr = [
      { area: "GZ", name: "YZW", age: 27 },
      { area: "GZ", name: "TYJ", age: 25 },
      { area: "SZ", name: "AAA", age: 23 },
      { area: "FS", name: "BBB", age: 21 },
      { area: "SZ", name: "CCC", age: 19 }
    ];
    

    group(arr, key = "area") {
      return arr.reduce((t, v) => {
        t[v[key]] ? t[v[key]].push(v) : (t[v[key]] = []) && t[v[key]].push(v);
        return t;
      }, {});
    }
    let aa = group(arr, "area");
    console.log(aa);//
    
    //{
        "GZ":[{"area":"GZ","name":"YZW","age":27},{"area":"GZ","name":"TYJ","age":25}],
        "SZ":[{"area":"SZ","name":"AAA","age":23},{"area":"SZ","name":"CCC","age":19}],
        "FS":[{"area":"FS","name":"BBB","age":21}]
       }

        </pre>
    </el-main>
  </div>
</template>
<script>
export default {
  data() {
    return {};
  },
  mounted() {
    const arr = [
      { area: "GZ", name: "YZW", age: 27 },
      { area: "GZ", name: "TYJ", age: 25 },
      { area: "SZ", name: "AAA", age: 23 },
      { area: "FS", name: "BBB", age: 21 },
      { area: "SZ", name: "CCC", age: 19 }
    ];
    let aa = this.group(arr, "area");
    console.log(JSON.stringify(aa));
  },
  methods: {
    group(arr, key = "area") {
      return arr.reduce((t, v) => {
        t[v[key]] ? t[v[key]].push(v) : (t[v[key]] = []) && t[v[key]].push(v);
        return t;
      }, {});
    },
    Group1(arr = [], key) {
      return arr.reduce(
        (t, v) => (!t[v[key]] && (t[v[key]] = []), t[v[key]].push(v), t),
        {}
      );
    },

    goPath(item) {
      this.$router.push({
        path: item.path
      });
    }
  }
};
</script>
